package com.iocaop.uaa.domain.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.With;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Set;


@With
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Table(name = "lzc_users")
public class User implements UserDetails,Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(length = 50,nullable = false,unique = true)
    private String username;

    @JsonIgnore
    @Column(name = "password_hash",length = 255,nullable = false)
    private String password;

    @Column(length = 255,unique = true,nullable = false)
    private String email;

    @Column(length = 255,unique = true,nullable = false)
    private String mobile;

    @Column(length = 50)
    private String name;

    @Column(nullable = false)
    private boolean enabled;

    @Column(name="account_non_expired",nullable = false)
    private boolean accountNonExpired;

    @Column(name="account_non_locked",nullable = false)
    private boolean accountNonLocked;

    @Column(name="credentials_non_expired",nullable = false)
    private boolean credentialsNonExpired;

    // 创建中间表
    @ManyToMany
    @Fetch(FetchMode.JOIN)
    @JoinTable(name = "lzc_users_roles",
        // 外键 当前类对应的表的列id
        joinColumns={@JoinColumn(name = "user_id",referencedColumnName = "id")},
        // 外键 Role对应的表的列id
    inverseJoinColumns = {@JoinColumn(name = "role_id",referencedColumnName ="id" )})

    private Set<Role> authorities;

//   在lombok中，如果成员变量是boolean类型的基础类型，自定生成的方法就是会is开头的方法。所以一下方法不进行重写也不会报错，这里就直接注释了。
//    @Override public Collection<? extends GrantedAuthority> getAuthorities() {
//        return null;
//    }

//    @Override public boolean isAccountNonExpired() {
//        return this.accountNonExpired;
//    }
//
//    @Override public boolean isAccountNonLocked() {
//        return this.accountNonLocked;
//    }
//
//    @Override public boolean isCredentialsNonExpired() {
//        return this.credentialsNonExpired;
//    }
//
//    @Override public boolean isEnabled() {
//        return this.enabled;
//    }
}
